﻿using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class TopicSystem : MonoBehaviour
{
    public static TopicSystem Instance;
    private TopicPanel topicPanel = null;
    public bool IsUpLoad = false;
    TopicDM topicDM1 = null;
    Dictionary<string, TopicDM> TopicDMPool = new Dictionary<string, TopicDM>();

    private void Awake()
    {
        Instance = this;
    }

    public void SetTopicDM(string Path)
    {
        TopicDM TempTopic = null;
        if (TopicDMPool.ContainsKey(Path))
        {
            TempTopic = TopicDMPool[Path];
            
        }
        else
        {
            TempTopic = ReadJson(Path);
            if (TempTopic!=null)
            {
                TopicDMPool.Add(Path, TempTopic);
               
            }
        }
        topicDM1 = TempTopic;
    }

    /// <summary>
    /// 获取玩家分数
    /// </summary>
    /// <returns></returns>
    public int GetPlayerScore()
    {
        int scoreTemp = 0;
        if (topicDM1 != null)
        {
            if (IsUpLoad == true)
            {
                for (int i = 0; i < topicDM1.topicItemDMs.Count; i++)
                {
                    if (topicDM1.topicItemDMs[i].IsTrue == true)
                    {
                        scoreTemp += topicDM1.topicItemDMs[i].MaxScore;
                        Debug.Log(topicDM1.topicItemDMs[i].TopicTytle + ": + " + topicDM1.topicItemDMs[i].MaxScore + ";总分：" + scoreTemp);
                    }
                }
            }
            else
            {
                Debug.Log("IsUpLoad=false无法计算分数");
            }
        }
        else
        {
            Debug.Log("topicDM1!=null无法计算成绩");
        }
        return scoreTemp;
    }

    public TopicDM GetTopicDM()
    {
        if (topicDM1 == null)
        {
            Debug.Log("读取的数据为空");
        }
        return topicDM1;
    }

    private TopicDM ReadJson(string Path)
    {
        string str = EzFrameWork.EzDataRWS.TxtFileRead(Path, Encoding.UTF8);
        TopicDM topicDM = JsonUtility.FromJson<TopicDM>(str);
        return topicDM;
    }

    private TopicItemDM GetTopicItemDMOld()
    {
        if (topicDM1 != null)
        {
            return topicDM1.topicItemDMs[topicDM1.TopicItemIndex];
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 返回第一题
    /// </summary>
    /// <returns></returns>
    private TopicItemDM GetTopicItemDMFirst()
    {
        if (topicDM1 != null)
        {
            
            Debug.Log("显示第一题");
            if (topicDM1.topicItemDMs.Count > 0)
            {
                return topicDM1.topicItemDMs[0];
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    private TopicItemDM GetTopicItemDMNext()
    {
        if (topicDM1 != null)
        {

            if (topicDM1.TopicItemIndex < topicDM1.topicItemDMs.Count - 1)
            {
                topicDM1.TopicItemIndex++;
            }
            else
            {
                topicDM1.TopicItemIndex = 0;
            }
            return topicDM1.topicItemDMs[topicDM1.TopicItemIndex];
        }
        else
        {
            return null;
        }
    }

    private TopicItemDM GetTopicItemDMLast()
    {
        if (topicDM1 != null)
        {
            if (topicDM1.TopicItemIndex > 0)
            {
                topicDM1.TopicItemIndex--;
            }
            else
            {
                topicDM1.TopicItemIndex = topicDM1.topicItemDMs.Count - 1;
            }
            return topicDM1.topicItemDMs[topicDM1.TopicItemIndex];
        }
        else
        {
            return null;
        }
    }

    public TopicItemDM GetTopicItemDMbyTytle(string Tytle)
    {
        TopicItemDM topicItemDM = null;
        if (topicDM1 != null)
        {
            for (int i = 0; i < topicDM1.topicItemDMs.Count; i++)
            {
                if (topicDM1.topicItemDMs[i].TopicTytle == Tytle)
                {
                    topicItemDM = topicDM1.topicItemDMs[i];
                    return topicItemDM;
                }
            }
            Debug.Log("没有找到：" + Tytle);
        }
        else
        {
            return topicItemDM;
        }
        return topicItemDM;
    }

    private void ShowTopicPanel(TopicItemDM topicItemDM)
    {
        if (topicItemDM != null)
        {
            if (topicPanel == null)
            {
                topicPanel = EzAssetManager.Instance.CreateObjFromRes("TopicPanel", transform).GetComponent<TopicPanel>();
            }
            topicPanel.SetData(topicItemDM);
        }
        else
        {
            Debug.Log("TopicItemDM为空");
        }
    }

    /// <summary>
    /// 结算答案对错
    /// </summary>
    public void GetScore()
    {
        IsUpLoad = true;
        TogglLockAll(false);//所有选项不能再修改
        //重新加载
        if (topicPanel != null)   //删除面板
        {
            ToolUnity.Destroy(topicPanel.gameObject);
            topicPanel = null;
        }
        ShowTopicPanel(GetTopicItemDMFirst());
    }

    /// <summary>
    /// 显示上次退出的位置,Path为问题路径
    /// </summary>
    public void ShowOldTopicPanel(string Path)
    {
        SetTopicDM(Path);
        ShowTopicPanel(GetTopicItemDMOld());
    }

    /// <summary>
    /// 显示第一页位置
    /// </summary>
    /// <param name="Path"></param>
    public void ShowFirstTopicPanel(string Path)
    {
        SetTopicDM(Path);
        ShowTopicPanel(GetTopicItemDMFirst());
    }

    public void ShowNextTopicPanel()
    {
        if (topicDM1!= null)
        {
            ShowTopicPanel(GetTopicItemDMNext());
        }
    }

    public void ShowLastTopicPanel()
    {
        if (topicDM1 != null)
        {
            ShowTopicPanel(GetTopicItemDMLast());
        }
    }

    /// <summary>
    /// 无法再继续选取
    /// </summary>
    /// <param name="Tytle"></param>
    public void ToggleLockByTytle(string Tytle, bool b)
    {
        if (topicDM1 != null)
        {
            for (int i = 0; i < topicDM1.topicItemDMs.Count; i++)
            {
                if (topicDM1.topicItemDMs[i].TopicTytle == Tytle)
                {
                    topicDM1.topicItemDMs[i].Interactable = b;
                    Debug.Log("完成锁：" + Tytle);
                    return;
                }
            }
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("玩家得分为：" + GetPlayerScore());
        }
    }

    /// <summary>
    /// 所有的选项都无法进行选取
    /// </summary>
    /// <param name="b"></param>
    public void TogglLockAll(bool b)
    {
        if (topicDM1 != null)
        {
            for (int i = 0; i < topicDM1.topicItemDMs.Count; i++)
            {
                topicDM1.topicItemDMs[i].Interactable = b;
            }
        }
    }
}
